package com.hongye.house.service.impl;
import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.constant.ExamineStatusConstant;
import com.hongye.common.entity.UserHomeEntity;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.Query;
import com.hongye.common.utils.R;
import com.hongye.house.config.HongYeESConfig;
import com.hongye.house.dao.ExamineHomeDao;
import com.hongye.house.entity.ExamineHomeEntity;
import com.hongye.house.fegin.ProprietaryFeign;
import com.hongye.house.service.ExamineHomeService;
import com.hongye.house.vo.HongyeOrderVo;
import com.hongye.common.vo.RequirementVo;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Service("examineHomeService")
public class ExamineHomeServiceImpl extends ServiceImpl<ExamineHomeDao, ExamineHomeEntity> implements ExamineHomeService {
    @Autowired
    ProprietaryFeign proprietaryFeign;
    @SuppressWarnings("all")
    @Autowired
    RestHighLevelClient client;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ExamineHomeEntity> page = this.page(
                new Query<ExamineHomeEntity>().getPage(params),
                new QueryWrapper<ExamineHomeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void closeExamine(HongyeOrderVo hongyeOrderVo) {
        ExamineHomeEntity homeEntity = baseMapper.selectOne(new QueryWrapper<ExamineHomeEntity>().eq("order_id", hongyeOrderVo.getOrderId()));
        homeEntity.setExamineStatus(ExamineStatusConstant.EXAMINE_NO_PASSED);
        baseMapper.updateById(homeEntity);
    }

    @Override
    public String getOrderSnByExamineId(Integer examineId) {
        ExamineHomeEntity examineHomeEntity = baseMapper.selectById(examineId);
        Integer orderId = examineHomeEntity.getOrderId();
        R info = proprietaryFeign.info(orderId);
        return JSON.toJSONString(info.get("order"));
    }

    @Override
    public R requirementToEs() {
        List<RequirementVo> requirementVos = baseMapper.requirementToEs();
        List<UserHomeEntity> userHomeEntityList = proprietaryFeign.getlist();
        requirementVos.forEach(requirementVo -> {
            for (UserHomeEntity userHomeEntity : userHomeEntityList) {
                if (requirementVo.getRequirementUserid().equals(userHomeEntity.getUserId())){
                    requirementVo.setUserName(userHomeEntity.getUserName());
                    requirementVo.setPhone(userHomeEntity.getUserPhone());
                }
            }
        });
        for (RequirementVo requirementVo : requirementVos) {
            IndexRequest indexRequest = new IndexRequest("requirement");
            indexRequest.id(requirementVo.getRequirementId()+"");
            indexRequest.source(JSON.toJSONString(requirementVo), XContentType.JSON);
            try {
                client.index(indexRequest, HongYeESConfig.COMMON_OPTIONS);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.ok();
    }

}